#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;

struct Edge{
    ll v, w;
};
vector<Edge> G[maxn];
int N, M, S;
//ll d[maxn];

ll dfs(int u, int fa){
    if(G[u].size() == 1 && u!=fa) return G[u][0].w;
//    ll &ans = d[u] = 0;
    ll ans = 0;
    for(auto &e:G[u])if(e.v!=fa){
        ans += min(dfs(e.v, u), e.w);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    cin >> N >> M >> S;
    for (int i = 0; i < N - 1; ++i) {
        int a, b, w; cin >> a >> b >> w;
        G[a].push_back({b, w});
        G[b].push_back({a, w});
    }

    cout << dfs(S, S);
    return 0;
}